'''
Created on 28/03/2010

@author: miguel
'''
import pygame
from pygame.sprite import Sprite
from vec2d import vec2d
from GameBoard import GameBoard

class Tree(Sprite):
    (INTACT, HALF_DESTROYED, DESTROYED) = range(3)
    TREE_IMAGE_INTACT = './data/tree_intact.png'
    TREE_IMAGE_HALF_DESTROYED = './data/tree_half_destroyed.png'


    def __init__(self, screen, game, field, position):
        Sprite.__init__(self)

        self.state = self.INTACT
        self.game = game
        self.pos = vec2d(position)
        self.image_intact = pygame.image.load(self.TREE_IMAGE_INTACT)
        self.image_half_destroyed = pygame.image.load(self.TREE_IMAGE_HALF_DESTROYED)
        self.image = self.image_intact
        self.field = field
        self.screen = screen
        self.draw_rect = self.image.get_rect().move(self.pos)
        self.rect = self.draw_rect

        self.game.game_board.take_position(vec2d(position), GameBoard.TREE)

    def hit_tree(self, position_list):
        if self.pos == position_list[0]:
            self.state = self.DESTROYED
            self.game.game_board.restore_position(self.pos, GameBoard.FREE)
        elif self.pos == position_list[1] or self.pos == position_list[2] or self.pos == position_list[3] or self.pos == position_list[4]:
            if self.state == self.INTACT:
                self.state = self.HALF_DESTROYED
                self.image = self.image_half_destroyed
            elif self.state == self.HALF_DESTROYED:
                self.state = self.DESTROYED
                self.game.game_board.restore_position(self.pos, GameBoard.FREE)

    def update(self):
        pass

    def draw(self):
        if self.state == self.INTACT or self.state == self.HALF_DESTROYED:
            self.screen.blit(self.image, self.draw_rect)
        elif self.state == self.DESTROYED:
            self.kill()